package com.xy.rest.http.controller;
import com.xy.common.base.controller.BaseController;
import com.xy.common.base.entity.AjaxResult;
import com.xy.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/rest/http/")
public class RestHttpController extends BaseController {
    private RestTemplate SimpleRestTemplate= SpringUtils.getBean("RestTemplateSimple");
    private RestTemplate RestTemplateRandom= SpringUtils.getBean("RestTemplateRandom");
    @Autowired
    private DiscoveryClient discoveryClient;
    /**
     * 调用 Nacos 配置的ID
     */
    private String serviceId="JmUser";

    /**
     * 这是简单调用
     * @return
     */
    @RequestMapping("simple/dome")
    public AjaxResult SimpleDome(){
        List<ServiceInstance> list =discoveryClient.getInstances(serviceId);
        ServiceInstance serviceInstance = list.get(0);
        return SimpleRestTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"jm/user/root/list",AjaxResult.class);
    }

    /**
     * 这是开启服务名称调用
     * @return
     */
    @RequestMapping("service/id/dome")
    public AjaxResult ServiceIdDome(){
        return RestTemplateRandom.getForObject(String.format("http://%s/jm/user/root/list",serviceId),AjaxResult.class);
    }
}
